using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.HttpApi.SimplyClient.Core;
using Joy.Erp.BasicArchives.Asns;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Joy.Erp.BasicArchives.HttpApi.SimplyClient.Asns
{
    public class AsnClientProxy
    {
        public readonly TokenClientProxy TokenClientProxy;

        public AsnClientProxy(TokenClientProxy tokenClientProxy)
        {
            TokenClientProxy = tokenClientProxy;
        }

        public async Task<AsnDto> GetAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/{id}");

            return await ApiHandler.HttpGetAsync<AsnDto>(token, url, logfunc);
        }

        public AsnDto Get(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnLineDto> GetChildAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/child/{id}");

            return await ApiHandler.HttpGetAsync<AsnLineDto>(token, url, logfunc);
        }

        public AsnLineDto GetChild(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetChildAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> FindAsync(string filter, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/find?filter={filter}");

            return await ApiHandler.HttpGetAsync<AsnDto>(token, url, logfunc);
        }

        public AsnDto Find(string filter, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindAsync(filter, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> FindBySourceKeyAsync(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/find-by-source-key?sourceKey={sourceKey}&sourceType={sourceType}");

            return await ApiHandler.HttpGetAsync<AsnDto>(token, url, logfunc);
        }

        public AsnDto FindBySourceKey(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindBySourceKeyAsync(sourceKey, sourceType, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<AsnDto>> GetListAsync(FilterRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn?filter={input.Filter}&sorting={input.Sorting}&skipCount={input.SkipCount}&maxResultCount={input.MaxResultCount}");

            return await ApiHandler.HttpGetAsync<PagedResultDto<AsnDto>>(token, url, logfunc);
        }

        public PagedResultDto<AsnDto> GetList(FilterRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetListAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<AsnDto>> QueryAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/query");

            return await ApiHandler.HttpPostAsync<PagedResultDto<AsnDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<AsnDto> Query(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<AsnLineDto>> QueryChildrenAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/query-children");

            return await ApiHandler.HttpPostAsync<PagedResultDto<AsnLineDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<AsnLineDto> QueryChildren(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryChildrenAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> CreateAsync(AsnCreateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn");

            return await ApiHandler.HttpPostAsync<AsnDto>(token, url, input, logfunc);
        }

        public AsnDto Create(AsnCreateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await CreateAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> UpdateAsync(Guid id, AsnUpdateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/{id}");

            return await ApiHandler.HttpPutAsync<AsnDto>(token, url, input, logfunc);
        }

        public AsnDto Update(Guid id, AsnUpdateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UpdateAsync(id, input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> SubmitAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/submit");

            var requestData = new { id };
            return await ApiHandler.HttpPostAsync<AsnDto>(token, url, requestData, logfunc);
        }

        public AsnDto Submit(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await SubmitAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> ApproveAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/approve");

            var requestData = new { id };
            return await ApiHandler.HttpPostAsync<AsnDto>(token, url, requestData, logfunc);
        }

        public AsnDto Approve(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await ApproveAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<AsnDto> UnApproveAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/unapprove");

            var requestData = new { id };
            return await ApiHandler.HttpPostAsync<AsnDto>(token, url, requestData, logfunc);
        }

        public AsnDto UnApprove(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UnApproveAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task DeleteAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/{id}");

            await ApiHandler.HttpDeleteAsync(token, url, logfunc);
        }

        public void Delete(Guid id, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await DeleteAsync(id, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<List<AsnDto>> BatchCreateAsync(List<AsnCreateDto> inputs, bool useBulk = false, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/batch-create?useBulk={useBulk.ToString().ToLower()}");

            return await ApiHandler.HttpPostAsync<List<AsnDto>>(token, url, inputs, logfunc);
        }

        public List<AsnDto> BatchCreate(List<AsnCreateDto> inputs, bool useBulk = false, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await BatchCreateAsync(inputs, useBulk, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<List<AsnDto>> BatchUpdateAsync(List<AsnBatchUpdateDto> inputs, bool useBulk = false, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/batch-update?useBulk={useBulk.ToString().ToLower()}");

            return await ApiHandler.HttpPutAsync<List<AsnDto>>(token, url, inputs, logfunc);
        }

        public List<AsnDto> BatchUpdate(List<AsnBatchUpdateDto> inputs, bool useBulk = false, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await BatchUpdateAsync(inputs, useBulk, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task BatchDeleteAsync(List<Guid> ids, bool useBulk = false, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/asn/batch-delete?useBulk={useBulk.ToString().ToLower()}");

            await ApiHandler.HttpPostAsync<object>(token, url, ids, logfunc);
        }

        public void BatchDelete(List<Guid> ids, bool useBulk = false, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await BatchDeleteAsync(ids, useBulk, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
} 
